home *** CD-ROM | disk | FTP | other *** search
/ Zoom 2 / Zoom - Release 2 (1996)(Active Software)[!].iso / programming / amiga / muibuilder / mb / developer / c / sources_gencodec / writecatalogfiles.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-17  |  10.0 KB  |  369 lines

  1. #include "WriteCatalogFiles.h"
  2. #include "Tools.h"
  3.  
  4. #include <stdio.h>
  5. #include <exec/types.h>
  6. #include <clib/dos_protos.h>
  7. #include <clib/exec_protos.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. extern void Quit(void);
  12.  
  13. /****************************************************************************************************************/
  14. /*****                                                                                                        *****/
  15. /**                                            WriteCatalogConstants                                                **/
  16. /*****                                                                                                        *****/
  17. /****************************************************************************************************************/
  18.  
  19. static BOOL WriteCatalogConstants(FILE *file,char *CatalogName)
  20. {
  21.     BPTR    TMPfile;
  22.     char    *file2;
  23.     char    *index;
  24.     char    *index2;
  25.     char    *variable;
  26.     __aligned struct FileInfoBlock  Info;
  27.     ULONG    length;
  28.  
  29.     if (TMPfile = Open(CatalogName, MODE_OLDFILE))
  30.     {
  31.         ExamineFH(TMPfile, &Info);
  32.         length = Info.fib_Size;
  33.         if (!(file2 = AllocMemory(length+1)))
  34.         {
  35.             fclose(file);
  36.             Close(TMPfile);
  37.             return FALSE;
  38.         }
  39.         Read( TMPfile, file2, length);
  40.         file2[length] = '\0';
  41.         Close(TMPfile);
  42.  
  43.         index = file2;
  44.         
  45.         while(index)
  46.         {
  47.             /* Search a descrition line */
  48.             /* Jump commands, comments lines */
  49.             while(index &&
  50.                   (*index=='#' || *index==';'))
  51.             {
  52.                 index = strchr(index,'\n');
  53.                 if (index)
  54.                     index++;
  55.             }
  56.  
  57.             /* Get description variable */ 
  58.             if (index)
  59.             {
  60.                 index2 = index;
  61.                 while(*index2 && *index2!=' ' && *index2!='\t' && *index2!='(') 
  62.                     index2++;
  63.                 if (*index2)
  64.                 {
  65.                     if (!(variable = AllocMemory(index2-index+1)))
  66.                     {
  67.                         fclose(file);
  68.                         FreeMemory(file2);
  69.                         return FALSE;
  70.                     }
  71.                     else
  72.                     {
  73.                         strncpy(variable,index,index2-index);
  74.                         variable[index2-index]='\0';
  75.                         fprintf(file,"extern const APTR _%s;\n",variable);
  76.                         fprintf(file,"#define %s ((APTR) &_%s)\n",variable,variable);
  77.                         FreeMemory(variable);
  78.                     }
  79.                     index2 = strchr(index2,'\n');
  80.                     
  81.                     /* Jump description string */
  82.                     do
  83.                     {
  84.                         index2++;
  85.                         index2 = strchr(index2,'\n');
  86.                     }while(index2 && *(index2-1)=='\\');
  87.                     if (index2)
  88.                         index = index2+1;
  89.                     else
  90.                         index = NULL;
  91.                 }
  92.                 else
  93.                     index = NULL;
  94.             }
  95.         }
  96.         FreeMemory(file2);
  97.         return TRUE;
  98.     }
  99.     else
  100.     {
  101.         DisplayMsg("Can't open catalog description file !!! \nPlease generate it with MUIBuilder !!! \n");
  102.         return FALSE;
  103.     }    
  104. }
  105.  
  106. /****************************************************************************************************************/
  107. /*****                                                                                                        *****/
  108. /**                                     WriteCatalogStringsInitialisation                                           **/
  109. /*****                                                                                                        *****/
  110. /****************************************************************************************************************/
  111.  
  112. static BOOL WriteCatalogStringsInitialisation(FILE *file,char *CatalogName)
  113. {
  114.     BPTR    TMPfile;
  115.     char    *file2;
  116.     char    *index;
  117.     char    *index2;
  118.     char    *variable;
  119.     __aligned struct FileInfoBlock  Info;
  120.     ULONG    length;
  121.     ULONG    cpt = 0;
  122.  
  123.     if (TMPfile = Open(CatalogName, MODE_OLDFILE))
  124.     {
  125.         ExamineFH(TMPfile, &Info);
  126.         length = Info.fib_Size;
  127.         if (!(file2 = AllocMemory(length+1)))
  128.         {
  129.             fclose(file);
  130.             Close(TMPfile);
  131.             return FALSE;
  132.         }
  133.         Read( TMPfile, file2, length);
  134.         file2[length] = '\0';
  135.         Close(TMPfile);
  136.  
  137.         index = file2;
  138.         
  139.         while(index)
  140.         {
  141.             /* Search a descrition line */
  142.             /* Jump commands, comments lines */
  143.             while(index &&
  144.                   (*index=='#' || *index==';'))
  145.             {
  146.                 index = strchr(index,'\n');
  147.                 if (index)
  148.                     index++;
  149.             }
  150.  
  151.             /* Get description variable */ 
  152.             if (index)
  153.             {
  154.                 index2 = index;
  155.                 while(*index2 && *index2!=' ' && *index2!='\t' && *index2!='(') 
  156.                     index2++;
  157.                 if (*index2)
  158.                 {
  159.                     if (!(variable = AllocMemory(index2-index+1)))
  160.                     {
  161.                         fclose(file);
  162.                         FreeMemory(file2);
  163.                         return FALSE;
  164.                     }
  165.                     else
  166.                     {
  167.                         strncpy(variable,index,index2-index);
  168.                         variable[index2-index]='\0';
  169.                         fprintf(file,"const struct FC_Type _%s = { %d, \"",variable,cpt++);
  170.                         FreeMemory(variable);
  171.  
  172.                         /* Get description string */
  173.                         index2 = strchr(index2,'\n');
  174.                         index = index2 + 1;
  175.                         do
  176.                         {
  177.                             index2++;
  178.                             index2 = strchr(index2,'\n');
  179.                         }while(index2 && *(index2-1)=='\\');
  180.  
  181.                         if (index2==0)                /* no "\n" in the last line of the file */
  182.                             index2=file2+length;    /* index2=end_of_file */
  183.  
  184.                         if (!(variable = AllocMemory(index2-index+1)))
  185.                         {
  186.                             fclose(file);
  187.                             FreeMemory(file2);
  188.                             return FALSE;
  189.                         }
  190.                         else
  191.                         {
  192.                             strncpy(variable,index,index2-index);
  193.                             variable[index2-index]='\0';
  194.                             fprintf(file,"%s\" };\n",variable);
  195.                             FreeMemory(variable);
  196.                             if (index2)
  197.                                 index = index2 + 1;
  198.                             else
  199.                             index = NULL;
  200.                         }                
  201.                     }
  202.                 }
  203.                 else
  204.                     index = NULL;
  205.             }
  206.         }
  207.         FreeMemory(file2);
  208.         return TRUE;
  209.     }
  210.     else
  211.     {
  212.         DisplayMsg("Can't open catalog description file !!! \nPlease generate it with MUIBuilder !!! \n");
  213.         return FALSE;
  214.     }    
  215. }
  216.  
  217. /****************************************************************************************************************/
  218. /*****                                                                                                        *****/
  219. /**                                         Write_Catalog_h_File                                                   **/
  220. /*****                                                                                                        *****/
  221. /****************************************************************************************************************/
  222.  
  223. BOOL Write_Catalog_h_File(char *Catalog_h_File,char *CatalogName,char *GetString)
  224. {
  225.     char    *name;
  226.     FILE    *file;
  227.  
  228.     if (file = fopen(Catalog_h_File,"w"))
  229.     {
  230.         if (!(name=AllocMemory(strlen(FilePart(CatalogName))+1)))
  231.         {
  232.             fclose(file);
  233.             return FALSE;
  234.         }
  235.         strcpy(name,FilePart(CatalogName));
  236.         remove_extend(name);
  237.  
  238.         fprintf(file,"#ifndef %s_CAT_H\n",name);
  239.         fprintf(file,"#define %s_CAT_H\n\n",name);
  240.         fprintf(file,"#include <exec/types.h>\n");
  241.         fprintf(file,"#include <libraries/locale.h>\n\n");
  242.         fprintf(file,"/* Prototypes */\n");
  243.         fprintf(file,"extern void OpenAppCatalog(struct Locale *, STRPTR);\n");
  244.         fprintf(file,"extern void CloseAppCatalog(void);\n");
  245.         fprintf(file,"extern char *%s(APTR);\n\n",GetString);
  246.  
  247.         fprintf(file,"/* Definitions */\n");
  248.         if (!WriteCatalogConstants(file,CatalogName))
  249.         {
  250.             FreeMemory(name);
  251.             fclose(file);
  252.             return FALSE;
  253.         }
  254.  
  255.         fprintf(file,"\n#endif /* !%s_CAT_H */\n",name);
  256.             
  257.         FreeMemory(name);
  258.         fclose(file);
  259.         return TRUE;
  260.     }
  261.     else
  262.     {
  263.         DisplayMsg("Unable to create Catalog_h File !!! \n");
  264.         return FALSE;
  265.     }
  266. }
  267.  
  268. /****************************************************************************************************************/
  269. /*****                                                                                                        *****/
  270. /**                                             Write_Catalog_c_File                                               **/
  271. /*****                                                                                                        *****/
  272. /****************************************************************************************************************/
  273.  
  274. BOOL Write_Catalog_c_File(char *Catalog_c_File,char *CatalogName,char *GetString)
  275. {
  276.     char    *name;
  277.     FILE    *file;
  278.  
  279.     if (file = fopen(Catalog_c_File,"w"))
  280.     {
  281.         if (!(name=AllocMemory(strlen(FilePart(CatalogName))+1)))
  282.         {
  283.             fclose(file);
  284.             return FALSE;
  285.         }
  286.         strcpy(name,FilePart(CatalogName));
  287.         remove_extend(name);
  288.  
  289.         fprintf(file,"/* Prototypes */\n");
  290.         fprintf(file,"#include <clib/locale_protos.h>\n");
  291.         fprintf(file,"#include <clib/dos_protos.h>\n\n");
  292.         fprintf(file,"/* Pragmas */\n");
  293.         fprintf(file,"#ifdef AZTEC_C\n");
  294.         fprintf(file,"#include <pragmas/locale_lib.h>\n");
  295.         fprintf(file,"#endif /*   AZTEC_C            */\n\n");
  296.         fprintf(file,"#if defined(__SASC)  ||  defined(_DCC)  ||  defined(__MAXON__)\n");
  297.         fprintf(file,"#include <pragmas/locale_pragmas.h>\n");
  298.         fprintf(file,"#endif    /*  __SASC  ||    _DCC  ||  __MAXON__    */\n\n");
  299.         fprintf(file,"#ifdef __GNUC__\n");
  300.         fprintf(file,"#include <inline/locale.h>\n");
  301.         fprintf(file,"#endif    /*  __GNUC__    */\n\n");
  302.         fprintf(file,"static LONG %s_Version = 0;\n",name);
  303.         fprintf(file,"static const STRPTR %s_BuiltInLanguage = (STRPTR) \"english\";\n\n",name);
  304.         fprintf(file,"struct FC_Type\n");
  305.         fprintf(file,"{   LONG   ID;\n");
  306.         fprintf(file,"    char *Str;\n");
  307.         fprintf(file,"};\n\n");
  308.  
  309.         fprintf(file,"/* Definitions */\n");
  310.         if (!WriteCatalogStringsInitialisation(file,CatalogName))
  311.         {
  312.             FreeMemory(name);
  313.             fclose(file);
  314.             return FALSE;
  315.         }
  316.  
  317.         fprintf(file,"static struct Catalog *%s_Catalog = NULL;\n\n",name);
  318.         fprintf(file,"void OpenAppCatalog(struct Locale *loc, STRPTR language)\n");
  319.         fprintf(file,"{\n");
  320.         fprintf(file,"\tLONG tag, tagarg;\n");
  321.         fprintf(file,"\textern struct Library *LocaleBase;\n");
  322.         fprintf(file,"\textern void CloseAppCatalog(void);\n\n");
  323.         fprintf(file,"\tCloseAppCatalog(); /* Not needed if the programmer pairs OpenAppCatalog\n");
  324.         fprintf(file,"\t\t\tand CloseAppCatalog right, but does no harm.  */\n\n");
  325.         fprintf(file,"\tif (LocaleBase != NULL  &&  %s_Catalog == NULL)\n",name);
  326.         fprintf(file,"\t{\n");
  327.         fprintf(file,"\tif (language == NULL)\n");
  328.         fprintf(file,"\t\ttag = TAG_IGNORE;\n");
  329.         fprintf(file,"\telse\n");
  330.         fprintf(file,"\t{\n");
  331.         fprintf(file,"\t\ttag = OC_Language;\n");
  332.         fprintf(file,"\t\ttagarg = (LONG) language;\n");
  333.         fprintf(file,"\t}\n");
  334.         remove_extend(CatalogName);
  335.         fprintf(file,"\t%s_Catalog = OpenCatalog(loc, (STRPTR) \"%s.catalog\",\n",name,FilePart(CatalogName));
  336.         add_extend(CatalogName,".cd");
  337.         fprintf(file,"\t\t\tOC_BuiltInLanguage, %s_BuiltInLanguage,\n",name);
  338.         fprintf(file,"\t\t\ttag, tagarg,\n");
  339.         fprintf(file,"\t\t\tOC_Version, %s_Version,\n",name);
  340.         fprintf(file,"\t\t\tTAG_DONE);\n");
  341.         fprintf(file,"\t}\n");
  342.         fprintf(file,"}\n\n");
  343.         fprintf(file,"void CloseAppCatalog(void)\n");
  344.         fprintf(file,"{\n");
  345.         fprintf(file,"\tif (LocaleBase != NULL)\n");
  346.         fprintf(file,"\t\tCloseCatalog(%s_Catalog);\n",name);
  347.         fprintf(file,"\t%s_Catalog = NULL;\n",name);
  348.         fprintf(file,"}\n\n");
  349.         fprintf(file,"char * %s(APTR fcstr)\n",GetString);
  350.         fprintf(file,"{\n");
  351.         fprintf(file,"\tchar *defaultstr;\n");
  352.         fprintf(file,"\tLONG strnum;\n\n");
  353.         fprintf(file,"\tstrnum = ((struct FC_Type *) fcstr)->ID;\n");
  354.         fprintf(file,"\tdefaultstr = ((struct FC_Type *) fcstr)->Str;\n\n");
  355.         fprintf(file,"\treturn(%s_Catalog ? (char *)GetCatalogStr(%s_Catalog, strnum, defaultstr) :\n",name,name);
  356.         fprintf(file,"\t\t\tdefaultstr);\n");
  357.         fprintf(file,"}\n");
  358.  
  359.         FreeMemory(name);
  360.         fclose(file);
  361.         return TRUE;
  362.     }
  363.     else
  364.     {
  365.         DisplayMsg("Unable to create Catalog_h File !!! \n");
  366.         return FALSE;
  367.     }
  368. }
  369.